/*
 * File Name:               InputAngle.cs
 * 
 * Description:             普通类
 * Author:                  chengyongxin
 * Create Date:             2016/12/12
 */

using UnityEngine;

public class InputAngle
{
    public static InputAngle inst = new InputAngle();

    public XVector2[] inputRestrictDir = new XVector2[16];

    public InputAngle()
    {
        for (int i = 0; i < 16; ++i)
        {
            XVector3 restrictDir = XQuaternion.Euler(0, 0, i * XNumber.create(22, 500)) * XVector3.right;
            inputRestrictDir[i].x = restrictDir.x;
            inputRestrictDir[i].y = restrictDir.y;
        }
    }

    public XVector2 FindNearestInputDir(XVector2 inputDir)
    {
        if (XIntMath.Approximately(inputDir.x, 0) && XIntMath.Approximately(inputDir.y, 0))
        // if(inputDir.x == 0 && inputDir.y == 0)
        {
            return XVector2.zero;
        }

        XNumber maxDotVal = -1;
        int maxIdx = 0;
        for (int i = 0; i < 16; ++i)
        {
            XNumber dot = XVector2.Dot(inputDir, inputRestrictDir[i]);
            if (dot > XNumber.create(0, 980))
            {
                maxDotVal = dot;
                maxIdx = i;
                break;
            }

            if (maxDotVal < dot)
            {
                maxDotVal = dot;
                maxIdx = i;
            }
        }

        return inputRestrictDir[maxIdx];
    }
}
